package com.sls.web.manager.recharge.service.impl;

import java.util.UUID;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.recharge.pojo.Dict;
import com.sls.web.manager.recharge.querybean.DictQuery;
import com.sls.web.manager.recharge.service.DictService;
import com.sls.web.manager.socket.SuperService;

@Service("dictService")
public class DictServiceImpl extends SuperService implements DictService {
	private Gson gson = CommonUtil.gson();

	@Override
	public void saveDict(Dict dict, UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.INSERT_DICT);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto;
		dto = super.send(head, dict);
		head = dto.getHead();
		if (!Constants.SUCCESS.equals(head.getResponseCode())) {
			throw new ManagerException(head.getResponseMessage());
		}
	}

	@Override
	public void deleteDict(Dict dict, UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.DELETE_DICT);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, dict);
		head = dto.getHead();
		if (!Constants.SUCCESS.equals(head.getResponseCode())) {
			throw new ManagerException(head.getResponseMessage());
		}
	}

	@Override
	public void updateDict(Dict dict, UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.UPDATE_DICT);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, dict);
		head = dto.getHead();
		if (!Constants.SUCCESS.equals(head.getResponseCode())) {
			throw new ManagerException(head.getResponseMessage());
		}

	}

	@Override
	public Dict findDictById(String id, UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.FIND_DICT_BY_ID);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		Dict dict = new Dict();
		dict.setId(id);
		SocketDTO dto = super.send(head, dict);
		head = dto.getHead();
		if (!Constants.SUCCESS.equals(head.getResponseCode())) {
			throw new ManagerException(head.getResponseMessage());
		}
		return gson.fromJson(dto.getBody(), Dict.class);
	}

	@Override
	public Pagination<Dict> findPage(DictQuery dictQuery, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.FIND_PAGE_OF_DICT);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, dictQuery);
		head = dto.getHead();
		if (!Constants.SUCCESS.equals(head.getResponseCode())) {
			throw new ManagerException(head.getResponseMessage());
		}
		return gson.fromJson(dto.getBody(), new TypeToken<Pagination<Dict>>() {
		}.getType());
	}

}
